home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-13 | 6.2 KB | 316 lines | [TEXT/ttxt] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- class FIXED_ARRAY[E]
- --
- -- Unlike ARRAY, the `lower' bound of a FIXED_ARRAY is frozen
- -- to 0. Thus, some memory is saved and looping toward `lower'
- -- bound (which is 0) run a little bit faster.
- --
-
- inherit ARRAYED_COLLECTION[E];
-
- creation
- make, with_capacity, from_collection
-
- feature
-
- lower: INTEGER is 0;
- -- Frozen lower bound.
-
- feature -- Creation and modification :
-
- make(size: INTEGER) is
- -- Make array with range [0 .. size - 1].
- -- When size = 0 the array is empty.
- require
- size >= 0
- do
- if size = 0 then
- upper := -1;
- elseif capacity = 0 then
- storage.calloc(size);
- capacity := size;
- upper := size - 1;
- elseif capacity < size then
- storage.realloc(storage,size);
- capacity := size;
- upper := size - 1;
- clear_all;
- else
- upper := size - 1;
- clear_all;
- end;
- ensure
- upper = size - 1;
- capacity >= old capacity;
- all_cleared
- end;
-
- with_capacity(needed_capacity: INTEGER) is
- -- Create an empty array with at least `needed_capacity'.
- do
- if capacity < needed_capacity then
- if capacity = 0 then
- storage.malloc(needed_capacity);
- else
- storage.realloc(storage,needed_capacity);
- end;
- capacity := needed_capacity;
- end;
- upper := -1;
- ensure
- capacity >= needed_capacity;
- empty
- end;
-
- feature -- Modification :
-
- resize(new_count: INTEGER) is
- -- Resize the array. When `new_count' is greater than
- -- `count', new positions are initialized with appropriate
- -- default values.
- require
- new_count >= 0
- local
- needed, i: INTEGER;
- elt_default: like item;
- do
- if new_count <= count then
- upper := new_count - 1;
- else
- needed := new_count;
- if capacity < needed then
- if capacity = 0 then
- storage.calloc(needed);
- else
- storage.realloc(storage,needed);
- end;
- capacity := needed;
- end;
- from
- needed := upper;
- upper := new_count - 1;
- i := upper;
- until
- i = needed
- loop
- put(elt_default,i);
- i := i - 1;
- end;
- end;
- ensure
- count = new_count;
- capacity >= old capacity
- end;
-
- feature
-
- sub_array(low, up: INTEGER): like Current is
- local
- i1, i2: INTEGER;
- do
- from
- i2 := up - low;
- !!Result.with_capacity(i2 + 1);
- Result.set_upper(i2);
- i2 := low;
- i1 := 0;
- until
- i2 > up
- loop
- Result.put(item(i2),i1);
- i1 := i1 + 1;
- i2 := i2 + 1;
- end;
- ensure then
- Result.lower = 0
- end;
-
- feature -- Implementation of deferred :
-
- item, infix "@" (index: INTEGER): E is
- do
- Result := storage.item(index);
- end;
-
- put(element: E; index: INTEGER) is
- do
- storage.put(element,index);
- end;
-
- add_first(element: like item) is
- do
- if upper + 1 <= capacity - 1 then
- upper := upper + 1;
- move(0,upper - 1,1);
- elseif capacity = 0 then
- storage.malloc(2);
- capacity := 2;
- upper := 0
- else
- capacity := 2 * capacity;
- storage.realloc(storage,capacity);
- upper := upper + 1;
- move(0,upper - 1,1);
- end;
- put(element,0);
- end;
-
- add_last(element: like item) is
- do
- if upper + 1 <= capacity - 1 then
- upper := upper + 1;
- elseif capacity = 0 then
- storage.malloc(2);
- capacity := 2;
- upper := 0
- else
- capacity := 2 * capacity;
- storage.realloc(storage,capacity);
- upper := upper + 1;
- end;
- put(element,upper);
- end;
-
- count: INTEGER is
- do
- Result := upper + 1;
- end;
-
- clear is
- do
- upper := -1;
- ensure then
- capacity = old capacity
- end;
-
- copy(other: like Current) is
- -- Copy `other' onto Current.
- do
- upper := other.upper;
- if upper >= 0 then
- if capacity = 0 then
- capacity := upper + 1;
- storage.malloc(capacity);
- elseif capacity < upper + 1 then
- capacity := upper + 1;
- storage.realloc(storage,capacity);
- end;
- storage.copy_from(other.storage,upper);
- end;
- end;
-
- set_all_with(v: like item) is
- do
- storage.set_all_with(v,upper);
- end;
-
- from_collection(model: COLLECTION[like item]) is
- local
- i1, i2, up: INTEGER;
- do
- from
- with_capacity(model.count);
- upper := model.count - 1;
- i1 := 0;
- i2 := model.lower;
- up := model.upper;
- until
- i2 > up
- loop
- put(model.item(i2),i1);
- i1 := i1 + 1;
- i2 := i2 + 1;
- end;
- end;
-
- is_equal(other: like Current): BOOLEAN is
- do
- if Current = other then
- Result := true;
- elseif upper = other.upper then
- if upper >= 0 then
- Result := storage.memcmp(other.storage,upper + 1);
- else
- Result := true;
- end;
- end;
- end;
-
- all_cleared: BOOLEAN is
- do
- Result := storage.all_cleared(upper);
- end;
-
- nb_occurrences(element: like item): INTEGER is
- do
- Result := storage.nb_occurrences(element,upper);
- end;
-
- fast_nb_occurrences(element: E): INTEGER is
- do
- Result := storage.fast_nb_occurrences(element,upper);
- end;
-
- index_of(element: like item): INTEGER is
- do
- Result := storage.index_of(element,upper);
- end;
-
- fast_index_of(element: like item): INTEGER is
- do
- Result := storage.fast_index_of(element,upper);
- end;
-
- slice(low, up: INTEGER): like Current is
- local
- i: INTEGER;
- do
- from
- i := low;
- !!Result.with_capacity(up - low + 1);
- until
- i > up
- loop
- Result.add_last(item(i));
- i := i + 1;
- end;
- end;
-
- force(element: E; index: INTEGER) is
- do
- if index <= upper then
- put(element,index);
- else
- resize(index + 1);
- put(element,index);
- end;
- end;
-
- remove_first is
- do
- if upper = 0 then
- storage.free;
- capacity := 0;
- upper := -1;
- else
- storage.remove_first(upper);
- upper := upper - 1;
- end;
- ensure then
- lower = old lower
- end;
-
- remove(index: INTEGER) is
- do
- storage.remove(index,upper);
- upper := upper - 1;
- end;
-
- invariant
-
- upper <= capacity;
-
- end -- FIXED_ARRAY[E]
-